﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//场景上有一个平面，有一个立方体，当鼠标点击选中立方体时，长按鼠标左键可以拖动立方体在平面上移动，点击鼠标右键取消选中

public class Lesson23_code2 : MonoBehaviour
{
    private Transform nowSelObj;
    public float offsetY;
    void Start()
    {
        
    }

    RaycastHit info;
    void Update()
    {
        //选中
        if (Input.GetMouseButtonDown(0))
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out info, 1000, 1 << LayerMask.NameToLayer("Monster")))
            {
                //记录选中对象
                nowSelObj = info.transform;
                Debug.LogFormat("选中 nowSelObj name:{0}", nowSelObj);
            }
        }

        //当前有选中对象 并且是 长按
        if (Input.GetMouseButton(0) && nowSelObj != null)
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out info, 1000, 1 << LayerMask.NameToLayer("Floor")))
            {
                nowSelObj.position = info.point + Vector3.up * offsetY;
            }
        }

        //取消
        if (Input.GetMouseButtonUp(0))
        {
            //取消记录
            Debug.LogFormat("取消 nowSelObj name:{0}", nowSelObj);
            nowSelObj = null;
        }
    }
}
